package com.yunkaweilai.android.activity.operation.integral;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.classic.common.MultipleStatusView;
import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.base.BaseApplication;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.constants.CommonConstants;
import com.yunkaweilai.android.interfaces.SimpleInterface;
import com.yunkaweilai.android.model.Event;
import com.yunkaweilai.android.model.integral.IntegralShopModel;
import com.yunkaweilai.android.model.login.UserInfoModel;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.MyToast;
import com.yunkaweilai.android.view.dialog.integral.IntegralDelDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 礼品查看
 * Created by Administrator on 2017/6/14.
 */

public class IntegralShopActivity extends BaseActivity implements SimpleInterface {


    @BindView(R.id.id_shop_img)
    ImageView idShopImg;
    @BindView(R.id.id_tv_shop_name)
    TextView idTvShopName;
    @BindView(R.id.id_tv_shop_type)
    TextView idTvShopType;
    @BindView(R.id.id_tv_shop_num)
    TextView idTvShopNum;
    @BindView(R.id.id_tv_sell_price)
    TextView idTvSellPrice;
    @BindView(R.id.id_tv_warning)
    TextView idTvWarning;
    @BindView(R.id.id_llayout_warning)
    LinearLayout idLlayoutWarning;
    @BindView(R.id.id_view_warning)
    View idViewWarning;
    @BindView(R.id.id_tv_commission)
    TextView idTvCommission;
    @BindView(R.id.id_llayout_commission)
    LinearLayout idLlayoutCommission;
    @BindView(R.id.content_view)
    ScrollView contentView;
    @BindView(R.id.id_multipleStatusView)
    MultipleStatusView idMultipleStatusView;
    @BindView(R.id.titlebar_iv_left)
    ImageView titlebarIvLeft;
    @BindView(R.id.titlebar_tv_left)
    TextView titlebarTvLeft;
    @BindView(R.id.titlebar_tv)
    TextView titlebarTv;
    @BindView(R.id.titlebar_iv_right_2)
    ImageView titlebarIvRight2;
    @BindView(R.id.titlebar_iv_right)
    ImageView titlebarIvRight;
    @BindView(R.id.titlebar_tv_right)
    TextView titlebarTvRight;
    @BindView(R.id.id_view_underline)
    View idViewUnderline;
    @BindView(R.id.rl_titlebar)
    RelativeLayout rlTitlebar;
    @BindView(R.id.id_llayout_del)
    LinearLayout idLlayoutDel;
    @BindView(R.id.id_del_views)
    View idDelViews;
    @BindView(R.id.id_tv_qiyong)
    TextView mTextview;
    @BindView(R.id.id_tv_zzdh)
    TextView mTextviewZzdh;
    @BindView(R.id.id_tv_remark)
    TextView mTextviewRemark;
    private String integral_id;
    public static final String INTEGRAL_ID = "INTEGRAL_ID";
    private IntegralShopModel shopInfoModel = new IntegralShopModel();//商品信息
    private UserInfoModel.DataBean.StoreEmployeeInfoBean userInfo;

    public static void actionStart(Context context, String shop_id) {
        Intent intent = new Intent(context, IntegralShopActivity.class);
        intent.putExtra(INTEGRAL_ID, shop_id);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_integral_shop);
        ButterKnife.bind(this);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        Intent intent = getIntent();
        integral_id = intent.getStringExtra(INTEGRAL_ID);
        userInfo = BaseApplication.getUserInfo();
        if (userInfo.isIs_first()) {
            idLlayoutDel.setVisibility(View.VISIBLE);
            idDelViews.setVisibility(View.VISIBLE);
            TitleBuilder titleBuilder = new TitleBuilder(this).setLeftImage(R.mipmap.ic_go_back).setTitleText("礼品详细");
//            if (appRuleModel.isIs_super() || appRuleModel.getApp_rule().get("21")) {
            titleBuilder.setRightText("修改");
//            }
        } else {
            idLlayoutDel.setVisibility(View.GONE);
            idDelViews.setVisibility(View.GONE);
            new TitleBuilder(this).setLeftImage(R.mipmap.ic_go_back).setTitleText("礼品详细");
        }

        idMultipleStatusView.showLoading();
        initHttp();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void refreshInfo(Event.TypeEvent event) {
        if (event != null) {
            if (event.type == CommonConstants.SHOP_INFO_REFRESH) {
                if (event.flag) {
                    idMultipleStatusView.showLoading();
                    initHttp();
                }
            }
        }
    }


    private void initHttp() {
        HttpRequest.postUrl(Api.APP_INTEGRAL_INFO)
                .addParams("id", integral_id)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(IntegralShopActivity.this, json);
                        if (checkCode) {
                            Gson gson = new Gson();
                            shopInfoModel = new IntegralShopModel();
                            shopInfoModel = gson.fromJson(json, IntegralShopModel.class);
                            initView();
                            if (idMultipleStatusView != null) {
                                idMultipleStatusView.showContent();
                            }
                        } else {
                            if (idMultipleStatusView != null) {
                                idMultipleStatusView.showError();
                            }
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        if (idMultipleStatusView != null) {
                            idMultipleStatusView.showError();
                        }
                    }
                });
    }

    //商品信息填充
    private void initView() {
        IntegralShopModel.DataBean.InfoBean info = shopInfoModel.getData().getInfo();
        if (mAc != null && !mAc.isFinishing()) {
            Utils.shopInfoImg(mAc, info.getGift_image(), idShopImg);
        }
        idTvShopName.setText(info.getGift_name() + "");//名称
        idTvShopType.setText("类别：" + info.getGoods_category_name() + "    条码：" + info.getGift_sku());
        idTvShopNum.setText("库存：" + info.getGift_stock() + "  已兑换：" + info.getUse_gift_stock());
        idTvSellPrice.setText(info.getPoints_price() + "");//兑换积分
        idTvWarning.setText(info.getValidity_start() + "至" + info.getValidity_end());//有效日期
        idTvCommission.setText(info.getSort());//排序
        mTextviewRemark.setText(info.getRemark());
        if (info.getStatus()!=null&&info.getStatus().equals("1")){
            mTextview.setText("使用中");
        }else {
            mTextview.setText("停用中");
        }
        if (info.getIs_app()!=null&&info.getIs_app().equals("1")){
            mTextviewZzdh.setText("使用中");
        }else {
            mTextviewZzdh.setText("停用中");
        }
    }

    @OnClick({R.id.titlebar_iv_left, R.id.titlebar_tv_right, R.id.id_shop_img, R.id.id_llayout_del})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.id_shop_img:
                Utils.ImageEnlage(mAc, "2", shopInfoModel.getData().getInfo().getGift_image(), "");
                break;
            case R.id.titlebar_iv_left:
                finish();
                break;
            case R.id.titlebar_tv_right:
                UpdateIntegralActivity.actionStart(this, integral_id);

                break;
            case R.id.id_llayout_del://删除商品
//                if (appRuleModel.isIs_super() || appRuleModel.getApp_rule().get("21")) {
                IntegralDelDialog dialog = new IntegralDelDialog(mAc, integral_id, this);
                dialog.show();
//                }else {
//                    showToast("没有删除权限");
//                }
                break;
        }
    }

    @Override
    public void nothing() {
        finish();
    }
}
